package me.stone.stanimeclient.core.ui.binding.adapters

import androidx.databinding.BindingAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener


@BindingAdapter("resourceTabs")
fun setResourcesTabs(
    tabLayout: TabLayout,
    resources: List<CharSequence>
) = tabLayout.run {
    removeAllTabs()
    resources.forEach { tabTitle ->
        addTab(newTab().apply {
            text = tabTitle
        })
    }
}

@BindingAdapter("selectedTabPos")
fun setTabSelectedTabPos(
    tabLayout: TabLayout, currentPos: Int
) {
    if (currentPos in 0 ..< tabLayout.tabCount) {
        tabLayout.selectTab(tabLayout.getTabAt(currentPos))
    }
}

@BindingAdapter("addTabSelectListener")
fun setTabSelectListener(
    tabLayout: TabLayout,
    listener: OnTabSelectedListener
) {
    tabLayout.clearOnTabSelectedListeners()
    tabLayout.addOnTabSelectedListener(listener)
}